package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.order.entity.OrderClientVolSum;
import cn.stylefeng.guns.modular.order.entity.OrderOutpreparePlatooncar;
import cn.stylefeng.guns.modular.order.model.params.OrderClientVolSumParam;
import cn.stylefeng.guns.modular.order.model.params.OrderOutpreparePlatooncarParam;
import cn.stylefeng.guns.modular.order.service.OrderClientVolSumService;
import cn.stylefeng.guns.modular.order.service.OrderOutpreparePlatooncarService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 出库订单预排车表控制器
 *
 * @author zhucx
 * @Date 2023-11-08 10:11:41
 */
@Controller
@RequestMapping("/orderOutpreparePlatooncar")
public class OrderOutpreparePlatooncarController extends BaseController {

    private String PREFIX = "/order/orderOutpreparePlatooncar";

    @Autowired
    private OrderOutpreparePlatooncarService orderOutpreparePlatooncarService;

    @Autowired
    private OrderClientVolSumService orderClientVolSumService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/orderOutpreparePlatooncar.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/orderOutpreparePlatooncar_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/orderOutpreparePlatooncar_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderOutpreparePlatooncarParam orderOutpreparePlatooncarParam) {
        this.orderOutpreparePlatooncarService.add(orderOutpreparePlatooncarParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderOutpreparePlatooncarParam orderOutpreparePlatooncarParam) {
        this.orderOutpreparePlatooncarService.update(orderOutpreparePlatooncarParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderOutpreparePlatooncarParam orderOutpreparePlatooncarParam) {
        OrderOutpreparePlatooncar orderOutpreparePlatooncar = this.orderOutpreparePlatooncarService.getById(orderOutpreparePlatooncarParam.getId());
        this.orderOutpreparePlatooncarService.delete(orderOutpreparePlatooncarParam);
        if(orderOutpreparePlatooncar.getClientVolSumId() != null){
            OrderClientVolSum orderClientVolSum = orderClientVolSumService.getById(orderOutpreparePlatooncar.getClientVolSumId());
            if(orderClientVolSum != null){
                OrderClientVolSumParam orderClientVolSumParam = new OrderClientVolSumParam();
                orderClientVolSumParam.setId(orderClientVolSum.getId());
                orderClientVolSumParam.setUndisVolume(orderClientVolSum.getVolume());
                orderClientVolSumService.update(orderClientVolSumParam);
            }

        }
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderOutpreparePlatooncarParam orderOutpreparePlatooncarParam) {
        OrderOutpreparePlatooncar detail = this.orderOutpreparePlatooncarService.getById(orderOutpreparePlatooncarParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-11-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderOutpreparePlatooncarParam orderOutpreparePlatooncarParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	orderOutpreparePlatooncarParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		orderOutpreparePlatooncarParam.setV1("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			orderOutpreparePlatooncarParam.setV1("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}

        // 1.承运商角色隔离
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam paramC = new BasicCarrierParam();
                    paramC.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(paramC, user.getOperateId());
                    if(bySpec != null){
                        orderOutpreparePlatooncarParam.setCarrierId(Long.valueOf(bySpec.getId()));
                    }else{
                        orderOutpreparePlatooncarParam.setCarrierId((long) -1);
                    }
                }
            }
        }

        return this.orderOutpreparePlatooncarService.findPageBySpec(orderOutpreparePlatooncarParam,condition,user.getOperateId());
    }

}


